//
//  BKOrderChooseCouponsView.m
//  bestkeep
//
//  Created by yons on 16/11/21.
//  Copyright © 2016年 utouu. All rights reserved.
//

#import "BKOrderChooseCouponsView.h"
#import "BK_OD_Color.h"
#import "BK_OD_Tool.h"
/**
    基类
 */
@implementation BKOrderChooseCouponsView


@end

/**
    使用说明
 */

NSString *const html = @"http://cdn.bestkeep.cn/ui/pc/agreement/coupon-agreement.html";

@implementation BKOrderDirectionsForUseView
{
    UIView *_BG;
    UIView *_centerBG;
    UILabel *_title;
    UIButton *_exitBtn;
    UIWebView *_content;
    UIImageView *_line;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self initialize];
    }
    return self;
}

- (void)initialize
{
    self.backgroundColor = [UIColor clearColor];
    
    _BG = [[UIView alloc] init];
    _BG.backgroundColor = [UIColor colorWithWhite:0 alpha:0.4];
    [self addSubview:_BG];
    
    _centerBG = [[UIView alloc] init];
    _centerBG.backgroundColor = kOrderWhite;
    [self addSubview:_centerBG];
    
    _title = [[UILabel alloc] init];
    _title.textColor = kOrderBlack1;
    _title.textAlignment = NSTextAlignmentCenter;
    _title.font = UIFontCustom(17);
    _title.layer.borderColor = kOrderLineGrey.CGColor;
    _title.text = kOrderLocalizeString(@"BKOrderDirectionsForUse");
    [self addSubview:_title];
    
    _exitBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _exitBtn.imageEdgeInsets = UIEdgeInsetsMake(7.5, 7.5, 7.5, 7.5);
    UIImage *image = [[UIImage imageNamed:@"BKOrderSpecificationClose"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
    [_exitBtn setImage:image forState:UIControlStateNormal];
    [_exitBtn setTintColor:kOrderPriceRed];
    [_exitBtn addTarget:self action:@selector(disappear) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_exitBtn];
    
    _content = [[UIWebView alloc] init];
    [_content loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:html]]];
    [self addSubview:_content];
    
    _line = [[UIImageView alloc] init];
    _line.backgroundColor = kOrderLineGrey;
    [self addSubview:_line];
    
    [self makeConstraints];
}

- (void)makeConstraints
{
    [_BG mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsMake(64, 0, 0, 0));
    }];
    
    [_centerBG mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.centerY.mas_equalTo(self);
        make.height.mas_equalTo(self).multipliedBy(0.7);
        make.width.mas_equalTo(self).multipliedBy(0.8);
    }];
    
    [_title mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.mas_equalTo(_centerBG);
        make.bottom.mas_equalTo(_centerBG.mas_top).offset(48);
    }];
    
    [_exitBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(_title);
        make.right.mas_equalTo(_title).offset(-2.5);
        make.width.height.mas_equalTo(30);
    }];
    
    [_content mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(_centerBG);
        make.top.mas_equalTo(_title.mas_bottom);
    }];
    
    [_line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.left.top.mas_equalTo(_content);
        make.height.mas_equalTo(1.0);
    }];
}

- (void)showInView:(UIView *)view
{
    self.frame = view.bounds;
    self.alpha = 0.1;
    [view addSubview:self];
    [UIView animateWithDuration:0.2 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        self.alpha = 1;
    } completion:^(BOOL finished) {
        
    }];
}

- (void)disappear
{
    [UIView animateWithDuration:0.3 animations:^{
        self.alpha = 0.1;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

@end











@implementation BKOrderExchangeCouponsView
{
    UITextField *_textField;
    UIButton    *_exchangeBtn;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self initialize];
    }
    return self;
}

- (void)initialize
{
    self.backgroundColor = [UIColor clearColor];
    
    _textField = [[UITextField alloc] init];
    _textField.borderStyle = UITextBorderStyleRoundedRect;
    _textField.font = UIFontCustom(13);
    _textField.placeholder = @"请输入6位兑换码（字母区分大小写）";
    [self addSubview:_textField];
    
    /*
     * 兑换  按钮
     */
    _exchangeBtn = [[UIButton alloc] init];
    [_exchangeBtn setTitle:kOrderLocalizeString(@"BKOrderExchange") forState:UIControlStateNormal];
    _exchangeBtn.titleLabel.font = UIFontNormal;
    _exchangeBtn.layer.cornerRadius = 5;
    _exchangeBtn.layer.masksToBounds = YES;
    UIImage *BGImage = [Common imageWithFrame:CGRectMake(0, 0, 1, 1) color:kOrderBKGreen];
    [_exchangeBtn setBackgroundImage:BGImage forState:UIControlStateNormal];
    [_exchangeBtn addTarget:self action:@selector(exchangeAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_exchangeBtn];
    
    [self makeConstraints];
}

- (void)makeConstraints
{
    [_textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.mas_equalTo(self).offset(ApplicationBackGauge);
        make.bottom.mas_equalTo(self);
        make.width.mas_equalTo(_exchangeBtn.mas_width).multipliedBy(6.0);
    }];
    
    [_exchangeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.height.mas_equalTo(_textField);
        make.left.mas_equalTo(_textField.mas_right).offset(5);
        make.right.mas_equalTo(self).offset(-ApplicationBackGauge);
    }];
}
/*
 * 点击兑换
 */
- (void)exchangeAction:(UIButton *)sender
{
    [self endEditing:YES];
    
    NSString *s = @"^[A-Za-z0-9]+$";
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",s];
    BOOL b = [predicate evaluateWithObject:_textField.text];
    
    if(_textField.text){
        if ([_textField.text isEqualToString:@""]) {
            kOrderPromptMsg(@"请输入兑换码");
            return;
        }
    }else{
        if (!_textField.text||[_textField.text isEqualToString:@""]) {
            kOrderPromptMsg(@"请输入兑换码");
            return;
        }
    }
    
    
    
    if (_textField.text.length != 6 || b == NO) {
        kOrderPromptMsg(@"请检查兑换码是否正确");
        return;
    }
    
    if (self.delegate && [self.delegate conformsToProtocol:@protocol(BKOrderExchangeCouponsViewDelegate)]) {
        [self.delegate orderViewExchangeCoupons:_textField.text];
    }
}

@end

